//
//  LZPlaceholderTextView.m
//  LZHundredJie
//
//  Created by apple on 16/5/1.
//  Copyright © 2016年 m14a.cn. All rights reserved.
//

/**
    通过代码造成的文字的改变,不会发出通知，发出通知必须是人为的
 */



#import "LZPlaceholderTextView.h"

@interface LZPlaceholderTextView ()

/** 占位文字label*/
@property (nonatomic, weak) UILabel *placeholderLabel;

@end

@implementation LZPlaceholderTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UILabel *placeholderLabel = [[UILabel alloc] init];
        placeholderLabel.numberOfLines = 0;
        [self addSubview:placeholderLabel];
        self.placeholderLabel = placeholderLabel;
        
        // 设置默认字体
        self.font = [UIFont systemFontOfSize:15.0];
        // 设置默认颜色
        self.placeholderColor = [UIColor grayColor];
        // 使用通知监听文字改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

- (void)textDidChange:(NSNotification *)note
{
    self.placeholderLabel.hidden = self.hasText;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = [placeholder copy];
    self.placeholderLabel.text = placeholder;
    [self.placeholderLabel sizeToFit];
    // 重新调用layoutSubviews
//    [self setNeedsLayout];
}
- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    self.placeholderLabel.font = font;
    [self.placeholderLabel sizeToFit];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    self.placeholderLabel.textColor = placeholderColor;
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    self.placeholderLabel.hidden = self.hasText;
}

- (void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    self.placeholderLabel.hidden = self.hasText;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.placeholderLabel.x = 5;
    self.placeholderLabel.y = 8;
    self.placeholderLabel.width = self.width - 2 * self.placeholderLabel.x;
    [self.placeholderLabel sizeToFit];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

//- (instancetype)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        // 设置默认字体
//        self.font = [UIFont systemFontOfSize:15.0];
//        // 设置默认颜色
//        self.placeholderColor = [UIColor grayColor];
//        // 使用通知监听文字改变
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
//    }
//    return self;
//}
//
//- (void)textDidChange:(NSNotification *)note
//{
//    // 会重新调用drawRect方法
//    [self setNeedsDisplay];
//}
//
//- (void)setPlaceholder:(NSString *)placeholder
//{
//    _placeholder = [placeholder copy];
//    // 会重新调用drawRect方法
//    [self setNeedsDisplay];
//}
//
//- (void)setFont:(UIFont *)font
//{
//    [super setFont:font];
//    // 会重新调用drawRect方法
//    [self setNeedsDisplay];
//}
//
//- (void)setPlaceholderColor:(UIColor *)placeholderColor
//{
//    _placeholderColor = placeholderColor;
//    // 会重新调用drawRect方法
//    [self setNeedsDisplay];
//}
//
//- (void)setText:(NSString *)text
//{
//    [super setText:text];
//    // 会重新调用drawRect方法
//    [self setNeedsDisplay];
//}
//
//- (void)setAttributedText:(NSAttributedString *)attributedText
//{
//    [super setAttributedText:attributedText];
//    // 会重新调用drawRect方法,
//    [self setNeedsDisplay];
//}
//
//
//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    // 会重新调用drawRect方法
//    [self setNeedsDisplay];
//}
//
///**
// *  每次调用drawRect方法:都会把以前画的东西清掉
// */
//// 这里的这个rect是bounds，这里bounds打印出来的y值是-64
//- (void)drawRect:(CGRect)rect
//{
//    // 如果有文字，就直接返回，不需要画占位文字
////    if (self.text.length || self.attributedText.length) return;
//    if (self.hasText) return;
//    
//    // 属性
//    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
//    attrs[NSFontAttributeName] = self.font;
//    attrs[NSForegroundColorAttributeName] = self.placeholderColor;
//
//    // 画文字
//    rect.origin.x = 5;
//    rect.origin.y = 8;
//    rect.size.width -= 2 * rect.origin.x;
//    [self.placeholder drawInRect:rect withAttributes:attrs];
//}
//
//- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}


